/******************************************************验证规则*****************************************************/
$.extend(_$,{
    "validator":{
        execute:function(value,nameCn,rule){

            var _this = this;
            var type = rule["type"];
            var validMsg = isEmpty(rule["msg"]) ? _this.messages[type] : rule["msg"];
            var ruleValue = rule['value'];

            var handler = _this.methods[type];

            var params={
                "value":value,
                "nameCn":nameCn,
                "ruleValue":ruleValue,
                "validMsg":validMsg
            };


            return handler.call(null,params);

        },
        addMethod:function(name,func,msg){
            _$.validator.methods[name] = func;
            _$.validator.messages[name] = msg;

        },
        messages:{
            required: "必填",
            email: "必须是有效的电子邮件地址",
            password:"必须由6-20位字母数字组成",
            mobile:"必须是有效的手机号",
            tel:"必须是有效的座机号",
            url: "必须是有效的网址",
            account: "必须是由6-10位字母和数字组成",
            money:"必须是有效的金钱格式",
            number:"必须是数字",
            integer: "必须是正整数",
            positive: "必须是整数",
            natural:'必须是自然数',
            min: "必须大于{1}",
            minEq: "必须大于等于{1}",
            max: "必须小于{1}",
            maxEq: "必须小于等于{1}",
            minLength: "必须大于{1}个字符",
            maxLength: "必须小于{1}个字符",
            minLengthEq: "必须大于等于{1}个字符",
            maxLengthEq: "必须小于等于{1}个字符",
            rangeLength: "长度必须是大{1},小于{2}",
            rangeLengthEq: "长度必须是大于{1},小于等于{2}",
            range:"必须大于{1},小于{2}",
            rangeEq:"必须是大于等于{1},小于等于{2}",
            equalTo:"必须等于[{1}] "
        },
        format:function(source, params ) {
            var _this = this;
            if ( arguments.length === 1 ) {
                return function() {
                    var args = $.makeArray( arguments );
                    args.unshift( source );
                    return _this.format.apply( this, args );
                };
            }
            if ( arguments.length > 2 && params.constructor !== Array  ) {
                params = $.makeArray( arguments ).slice( 1 );
            }
            if ( params.constructor !== Array ) {
                params = [ params ];
            }
            $.each( params, function( i, n ) {
                source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
                    return n;
                });
            });
            return source;
        },
        zeroHandler:function(params,func){

            var value=params["value"];
            var nameCn=params["nameCn"];
            var ruleValue=params["ruleValue"];
            var validMsg=params["validMsg"];

            var state = true;
            var info = "";

            if (isEmpty(value)) {
                state = true;
            }else{
                if (!func.call(null,value)) {
                    state = false;
                    var names = [nameCn]
                    info = _$.validator.format(validMsg, names);
                }
            }
            return {
                "state":state,
                "info":info
            };
        },
        oneHandler:function(params,func){
            var value=params["value"];
            var nameCn=params["nameCn"];
            var ruleValue=params["ruleValue"];
            var validMsg=params["validMsg"];

            var state = true;
            var info = "";

            if (isEmpty(value)) {
                state = true;
            }else{
                if (!func.call(null,value,ruleValue)) {
                    state = false;
                    var names = [nameCn,ruleValue]
                    info = _$.validator.format(validMsg, names);
                }
            }
            return {
                "state":state,
                "info":info
            };
        },
        twoHandler:function(params,func){

            var value=params["value"];
            var nameCn=params["nameCn"];
            var ruleValue=params["ruleValue"];
            var validMsg=params["validMsg"];
            var state = true;
            var info = "";

            if (isEmpty(value)) {
                state = true;
            }else{
                var arr = ruleValue.split(",");
                var min = arr[0];
                var max = arr[1];

                if (!func.call(null,value,min,max)) {
                    state = false;
                    var names = [nameCn,min,max]
                    info = _$.validator.format(validMsg, names);
                }
            }
            return {
                "state":state,
                "info":info
            };
        },
        methods: {
            'required': function (params) {

                var value=params["value"];
                var nameCn=params["nameCn"];
                var ruleValue=params["ruleValue"];
                var validMsg=params["validMsg"];

                var state = true;
                var info = "";

                if (isEmpty(value)) {
                    state = false;
                    var names = [nameCn]
                    info = _$.validator.format(validMsg, names);
                }

                return {
                    "state":state,
                    "info":info
                };
            },
            'email': function(params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                    return reg.test(val);
                });
            },
            'password': function(params){
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^[A-Za-z0-9]{6,20}$/;
                    return reg.test(val);
                });
            },

            'mobile': function (params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^1\d{10}$/;
                    return reg.test(val);
                });
            },
            'tel': function(params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^0\d{2,3}-?\d{7,8}$/;
                    return reg.test(val);
                });
            },
            'url': function(params){
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^((https|http|ftp|rtsp|mms)?:\/\/)+[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
                    return reg.test(val);
                });
            },
            'account': function(params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^[a-z0-9A-z]\w{5,9}$/;
                    return reg.test(val);
                });
            },
            'money': function(params){
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /(^-?[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
                    return reg.test(val);
                });
            },
            number: function(params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^(\-|\+)?\d+(\.\d+)?$/;
                    return reg.test(val);
                });
            },
            'integer': function (params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^[1-9]\d*$/;
                    return reg.test(val);
                });
            },
            'positive': function (params){
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^-?\d+$/;
                    return reg.test(val);
                });
            },
            'natural': function(params) {
                return  _$.validator.zeroHandler(params,function(val){
                    var reg = /^\d+$/;
                    return reg.test(val);
                });
            },
            'min': function(params) {
                return  _$.validator.oneHandler(params,function(val,num){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val > num) {
                        return true;
                    }
                    return false;
                });
            },
            'minEq': function (params) {
                return  _$.validator.oneHandler(params,function(val,num){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val >= num) {
                        return true;
                    }
                    return false;
                });
            },
            'max': function (params) {
                return  _$.validator.oneHandler(params,function(val,num){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val < num) {
                        return true;
                    }
                    return false;
                });
            },
            'maxEq': function (params) {
                return  _$.validator.oneHandler(params,function(val,num){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val <= num) {
                        return true;
                    }
                    return false;
                });
            },
            'minLength': function (params) {
                return  _$.validator.oneHandler(params,function(val,min){
                    var str = (val + "").trim();
                    if (str.length <= min) {
                        return false;
                    }
                    return true;
                });
            },
            'maxLength': function (params) {
                return  _$.validator.oneHandler(params,function(val,max){
                    var str = (val + "").trim();
                    if (str.length >= max) {
                        return false;
                    }
                    return true;
                });
            },

            'minLengthEq': function (params) {
                return  _$.validator.oneHandler(params,function(val,min){
                    var str = (val + "").trim();
                    if (str.length < min) {
                        return false;
                    }
                    return true;
                });
            },
            'maxLengthEq': function(params) {

                return  _$.validator.oneHandler(params,function(val,max){
                    var str = (val + "").trim();
                    if (str.length > max) {
                        return false;
                    }
                    return true;
                });
            },
            'rangeLength': function (params) {
                return  _$.validator.twoHandler(params,function(val,min,max){
                    var str = (val + "").trim();
                    if (str.length < max && str.length > min) {
                        return true;
                    }
                    return false;
                });
            },
            'rangeLengthEq': function (params) {
                return  _$.validator.twoHandler(params,function(val,min,max){

                    var str = (val + "").trim();
                    if (str.length <= max && str.length >= min) {
                        return true;
                    }
                    return false;
                });
            },
            'range': function (params) {
                return  _$.validator.twoHandler(params,function(val,min,max){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val < max && val > min) {
                        return false;
                    }
                    return true;
                });
            },
            'rangeEq': function (params) {
                return  _$.validator.twoHandler(params,function(val,min,max){
                    if (isNaN(val)) {
                        return false;
                    }
                    val = parseFloat(val);
                    if (val <= max && val >= min) {
                        return false;
                    }
                    return true;
                });
            },
            'equalTo': function (params){
                var value=params["value"];
                var nameCn=params["nameCn"];
                var compareTo=params["ruleValue"];
                var validMsg=params["validMsg"];

                var objTo = _$.getById(compareTo)

                var valueTo = objTo.getValue();
                var nameCnTo = objTo.getOption("nameCn");

                var state = true;
                var info = "";

                if (isEmpty(value)) {
                    state = true;
                }else{
                    if (!(value == valueTo)) {
                        state = false;
                        var names = [nameCn,nameCnTo];
                        info = _$.validator.format(validMsg, names);
                    }
                }
                return {
                    "state":state,
                    "info":info
                };
             }
        }
    }
});

/*
 _$.validator.addMethod("xxxx",function(params){
 return  _$.validator.zeroHandler(params,function(val){
 if (isNaN(val)) {
 return false;
 }
 val = parseFloat(val);
 if (val > 10) {
 return true;
 }
 return false;
 });
 },"[{0}]这是一个测试校验");


 */